package com.shangcheng.server.controller;

import com.shangcheng.model.domain.PmsProductAttributeCategory;
import com.shangcheng.model.vo.PageResult;
import com.shangcheng.model.vo.PmsProductAttributeCategoryVo;
import com.shangcheng.model.vo.result.Result;
import com.shangcheng.server.exception.BusinessException;
import com.shangcheng.server.service.PmsProductAttributeCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("productAttribute")
public class PmsProductAttributeCategoryController {
    @Autowired
    private PmsProductAttributeCategoryService pmsProductAttributeCategoryService;

    //添加商品属性分类
    @PostMapping("category/create")
    public ResponseEntity create(String name){
        pmsProductAttributeCategoryService.create(name);
        return ResponseEntity.ok(new Result(200,null,""));
    }

    //删除单个商品属性分类
    @GetMapping("category/delete/{id}")
    public ResponseEntity delete(@PathVariable Integer id){
        PmsProductAttributeCategory byId = pmsProductAttributeCategoryService.getById(id);
        if (byId.getAttributeCount()!=0||byId.getParamCount()!=0){
            throw new BusinessException("该分类下有属性,无法删除");
        }
        pmsProductAttributeCategoryService.removeById(id);
        return ResponseEntity.ok(new Result(200,null,""));
    }

    //分页获取所有商品属性分类
    @GetMapping("category/list")
    public ResponseEntity list(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "5") Integer pageSize){
       PageResult pageResult =  pmsProductAttributeCategoryService.getList(pageNum,pageSize);
        return ResponseEntity.ok(new Result(200,pageResult,""));
    }

    //获取所有商品属性分类及其下属性
    @GetMapping("category/list/withAttr")
    public ResponseEntity listWithAttr(){
        List<PmsProductAttributeCategoryVo> pmsProductAttributeCategoryVo =  pmsProductAttributeCategoryService.listWithAttr();
        return ResponseEntity.ok(new Result(200,pmsProductAttributeCategoryVo,""));
    }

    //修改商品属性分类
    @PostMapping("category/update/{id}")
    public ResponseEntity update(@PathVariable Integer id,String name){
        pmsProductAttributeCategoryService.updateProductAttributeCategoryById(id,name);
        return ResponseEntity.ok(new Result(200,null,""));
    }

    //获取单个商品属性分类信息
    @GetMapping("category/category/{id}")
    public ResponseEntity getCategoryById(@PathVariable Long id){
        PmsProductAttributeCategory category = pmsProductAttributeCategoryService.getCategoryById(id);
        return ResponseEntity.ok(new Result(200,category,""));
    }
}
